package org.campuscontent.model;

import org.campuscontent.ui.AutomataChameleonView;
import org.campuscontent.ui.EdgeMaskView;
import org.jgraph.graph.CellView;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.EdgeView;
import org.jgraph.graph.VertexView;

public class AutomataCellViewFactory extends DefaultCellViewFactory implements AutomataConstants
{
    public static void setNodeID( DefaultGraphCell cell, String ID )
    {
        cell.getAttributes().put( Global.ID, ID ); 
    }
    
    public static void setNodeName( DefaultGraphCell cell, String name )
    {
        cell.getAttributes().put( Global.NAME, name );
    }
    
    public static boolean hasName( DefaultGraphCell cell )
    {
        return cell.getAttributes().get( Global.NAME ) != null;
    }
    
    public static String getName( DefaultGraphCell cell )
    {
        return (String)cell.getAttributes().get( Global.NAME );
    }
    
    public static String getID( DefaultGraphCell cell )
    {
        return (String)cell.getAttributes().get( Global.ID );
    }
    
    public static boolean hasID( DefaultGraphCell cell, String ID )
    {
        String id = (String)cell.getAttributes().get( Global.ID ); 
        return id != null && id.equals( ID );
    }
    
    public static void setNodeType( DefaultGraphCell cell, NodeType type )
    {
        cell.getAttributes().put( Global.TYPE, type );
    }
    
    public static void updateViewType( CellView view, NodeType type )
    {
        view.getAllAttributes().put( Global.TYPE, type );
    }
    
    public static NodeType getNodeType( DefaultGraphCell cell )
    {
        return (NodeType)cell.getAttributes().get( Global.TYPE );
    }
    
    protected VertexView createVertexView( Object obj )
    {
        try
        {
            return new AutomataChameleonView( obj );
        }
        catch ( Exception ex )
        {
            ex.printStackTrace();
        }
        
        return super.createVertexView( obj );
    }

    protected EdgeView createEdgeView( Object cell ) 
    {
        try
        { 
            EdgeMaskView e = new EdgeMaskView( cell );
            
            return e;
        }
        catch ( Throwable ex )
        {
            ex.printStackTrace();
        }
        
        return super.createEdgeView( cell );
    }
}
